<?php

namespace App\Jobs;

use App\Models\PetsFoodLogsModel;
use App\Models\PetsModel;
use App\Models\UserModel;
use App\Tools\AliyunUploadTool;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class Ffmpeg implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**

     * 任务最大尝试次数。

     *

     * @var int

     */

    public $tries = 3;

    /**

     * 任务运行的超时时间。

     *

     * @var int

     */

    public $timeout = 300;


    protected $data;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($data)
    {
        $this->data = $data;

    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        writeLoToFile('jobs', 'Queue：', $this->data);
        $params = json_decode($this->data, true);

        //获取用户id和宠物id
        $foodLogs = PetsFoodLogsModel::getSingle(['id'=> $params['feed_logs_id']]);

        //获取宠物领养编号
        $pets = PetsModel::getSingle(['id'=> $foodLogs['pets_id']]);

        //获取用户昵称
        $user = UserModel::getSingle(['id'=> $foodLogs['user_id']]);
        writeLoToFile('jobs', 'Queue1：', json_encode($user));

        //下载视频, 保存本地路径
        $dir = '/www/test/';
        $textTitle = $dir . time() . rand(1000, 9999);
        $textName = $textTitle . 'handle';
        file_put_contents($textTitle . '.mp4', file_get_contents($params['video_url']));
        writeLoToFile('jobs', 'Queue2：', json_encode($textName));

        //视频加水印
        exec('/usr/local/ffmpeg/bin/ffmpeg -i ' . $textTitle . '.mp4 -vf "drawtext=fontfile=/usr/share/fonts/chinese/simhei.ttf: text=' . '洋洋农场' . ':x=10:y=10:fontsize=20:fontcolor=white:shadowy=2,drawtext=fontfile=/usr/share/fonts/chinese/simhei.ttf: text=' . '动物编号：' . $pets['adopt_number'] . '' . ':x=10:y=33:fontsize=20:fontcolor=white:shadowy=2,drawtext=fontfile=/usr/share/fonts/chinese/simhei.ttf: text=' . '所属用户：' . $user['nickname'] . '' . ':x=10:y=56:fontsize=20:fontcolor=white:shadowy=2" -c:v libx264 -c:a copy ' . $textName . '.mp4 2>&1',$array,$status);
        writeLoToFile('jobs', 'Queue3：', '加完水印');

        //上传加过水印的视频到oss
        //上传阿里云
        $qrName = time() . rand(1000, 9999) .'.mp4';
        $object = 'api/' . date('Ymd', time()) . '/' . $qrName;
        (new AliyunUploadTool())->uploadFile('farm-sheep', $object, $textName . '.mp4');

        //拼接oss地址
        $pix = config('wechat.oss.url');
        $petsQrcode = $pix . $object;
        writeLoToFile('jobs', 'Queue4：', json_encode($petsQrcode));

        //修改数据库
        PetsFoodLogsModel::updateSingle(['id'=> $params['feed_logs_id']], ['video_url'=> $petsQrcode]);

        writeLoToFile('jobs', 'Queue5：', '修改数据库');

        //删除本地视频
        unlink($textTitle . '.mp4');

        writeLoToFile('jobs', 'success：', '结束');
    }
}
